package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.StoreRecordProductMapper;
import com.zhijian.warehouse.manage.entity.po.StoreRecordProduct;
import com.zhijian.warehouse.manage.entity.vo.request.StoreRecordProductSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreRecordProductSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreRecordProductVo;
import com.zhijian.warehouse.manage.service.StoreRecordProductService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 入库产品明细表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-20 22:23:13
 */
@Slf4j
@Service
public class StoreRecordProductServiceImpl implements StoreRecordProductService {

    @Autowired
    private StoreRecordProductMapper mapper;

    @Override
    public StoreRecordProduct selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreRecordProductVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreRecordProduct storeRecordProduct) {
        Date now = new Date();
        storeRecordProduct.setCreateTime(now);
        storeRecordProduct.setUpdateTime(now);
        return mapper.insert(storeRecordProduct) > 0 ? storeRecordProduct.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreRecordProduct storeRecordProduct) {
        Date now = new Date();
        storeRecordProduct.setCreateTime(now);
        storeRecordProduct.setUpdateTime(now);
        return mapper.insertSelective(storeRecordProduct) > 0 ? storeRecordProduct.getId() : 0L;
    }

    @Override
    public boolean update(StoreRecordProduct storeRecordProduct) {
        Date now = new Date();
        storeRecordProduct.setUpdateTime(now);
        return mapper.updateByPrimaryKey(storeRecordProduct) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreRecordProduct record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreRecordProduct storeRecordProduct = new StoreRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeRecordProduct);
        return this.insert(storeRecordProduct) != 0;
    }

    @Override
    public boolean modify(StoreRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreRecordProduct old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreRecordProduct storeRecordProduct = new StoreRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeRecordProduct);
        return this.update(storeRecordProduct);
    }

    @Override
    public StoreRecordProductVo selectOne(StoreRecordProductSearchVo storeRecordProductSearchVo) {
        return mapper.selectOne(storeRecordProductSearchVo);
    }

    @Override
    public Integer count(StoreRecordProductSearchVo storeRecordProductSearchVo) {
        return mapper.count(storeRecordProductSearchVo);
    }

    @Override
    public List<StoreRecordProductVo> selectList(StoreRecordProductSearchVo storeRecordProductSearchVo) {
        return mapper.selectList(storeRecordProductSearchVo);
    }

    @Override
    public Page<StoreRecordProductVo> selectPage(StoreRecordProductSearchVo storeRecordProductSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeRecordProductSearchVo.getPageNum()) || Objects.isNull(storeRecordProductSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeRecordProductSearchVo);
        //分页
        Page<StoreRecordProductVo> page = new Page<>(true, count, storeRecordProductSearchVo.getPageNum(), storeRecordProductSearchVo.getPageSize());
        storeRecordProductSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreRecordProductVo> responseVos = this.selectList(storeRecordProductSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreRecordProduct> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StoreRecordProductSearchVo storeRecordProductSearchVo) {
        if (BeanUtil.isEmpty(storeRecordProductSearchVo.getStoreRecordId()))
            return;
        List<StoreRecordProductVo> storeRecordProductVos = this.selectList(storeRecordProductSearchVo);
        if (BeanUtil.isEmpty(storeRecordProductVos))
            return;
        //获取id
        List<Long> ids = storeRecordProductVos.stream().map(StoreRecordProductVo::getId).collect(Collectors.toList());
        this.deleteByIdList(ids);
    }
}